package com.cn.jx.manager.dao;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.cn.jx.manager.po.Role;

/**
 * <p>RoleRepository 角色mapper层操作数据库的接口
 * <p>实现<{@link BaseRepository <tt>公用</tt>}}
 * 
 * @author <h1>jiangxu2  2019年11月21日</h1>
 * @version v1.11.20.2019
 * @since 1.0
 * 
 */
public interface RoleRepository extends JpaRepository<Role,String>,JpaSpecificationExecutor<Role>{
	
	/**<p>定义HQL查询标记删除{@link Role} */
	@Modifying
	@Query("update Role c set c.delFlg=1,c.validFlg=0 where c.id=:pid ")
	public void deleteEntityById(@Param(value="pid") String pid);
	
	/**<p>定义HQL找回 恢复删除{@link Role} */
	@Modifying
	@Query("update Role c set c.delFlg=0,c.validFlg=1 where c.id=:pid ")
	public void recoverEntityById(@Param(value="pid") String pid);
	
	/**<p>定义HQL查询 根据用户id。userid查询User对应的角色信息 角色必须有效{@link Role}*/
	@Query("select r from Role r "
			+ " inner join UserRole ur on ur.roleId=r.id"
			+ " where ur.userId=:pid and r.delFlg=0 and r.validFlg=1 ")
	public List<Role> findRoleByUserId(@Param(value = "pid") String pid);
	
	/**<p>定义HQL查询 根据用户id。userid查询User对应的角色信息 角色必须有效{@link Role}*/
	@Query(" from Role where delFlg=0 and validFlg=1 " )
	public List<Role> findAllRole();
	
	@Query(" select r.roleName from Role r "
			+ "inner join UserRole ur on ur.roleId=r.id where ur.userId=:uid and r.delFlg=0 and r.validFlg=1 " )
	public List<String> findRoleNamesByUserId(@Param(value = "uid") String uid);
}
